const initialState = {
  channelList: [] as ChannelType[],
  userList: [] as ChannelType[],
  channel_id: 0,
};

export type ChannelType = {
  id: number;
  name: string;
};
export type ChannelActionType =
  | {
      type: 'channel/saveChannels';
      payload: ChannelType[];
    }
  | {
      type: 'channel/saveUserChannels';
      payload: ChannelType[];
    }
  | {
      type: 'channel/updateUserChannels';
      payload: ChannelType;
    }
  | {
      type: 'channel/delUserChannels';
      payload: number;
    }
  | {
      type: 'channel/setChannelId';
      payload: number;
    };
export default function Channel(
  state = initialState,
  action: ChannelActionType
): typeof initialState {
  switch (action.type) {
    case 'channel/saveChannels':
      return {
        ...state,
        channelList: action.payload,
      };
    case 'channel/saveUserChannels':
      return {
        ...state,
        userList: action.payload,
      };
    case 'channel/updateUserChannels':
      return {
        ...state,
        userList: [...state.userList, action.payload],
      };
    case 'channel/delUserChannels':
      return {
        ...state,
        userList: state.userList.filter((item) => item.id !== action.payload),
      };
    case 'channel/setChannelId':
      return {
        ...state,
        channel_id: action.payload,
      };
    default:
      return state;
  }
}
